package com.hhkj.admin.train.domain;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hhkj.admin.question.model.res.QuestionRes;
import com.hhkj.admin.train.common.cont.TrainConst;
import com.hhkj.admin.train.common.mybatis.handler.TrainStudentAnswerListHandler;
import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.cont.SysConst;
import com.ruoyi.common.core.domain.BizBaseEntity;
import com.ruoyi.common.valid.VEdit;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;
import java.util.List;

/**
 * 培训练习对象 t_train
 *
 * @author ruoyi
 * @since 2023-05-24
 */
@Data
@EqualsAndHashCode(callSuper = true)
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
@TableName(value = "t_train", autoResultMap = true)
public class Train extends BizBaseEntity
{
    private static final long serialVersionUID = 1L;

    /** 学员id,关联t_student表主键 */
    @Excel(name = "学员id")
    private Long studentId;

    /** 科目id，关联t_course表主键 */
    @Excel(name = "科目id")
    private Long courseId;

    /** 科目名称 */
    @TableField(exist = false)
    private String courseName;

    /** 题库id，关联t_bank表主键 */
    @Excel(name = "题库id，关联t_bank表主键")
    private Long bankId;

    /** 总题数 */
    @Excel(name = "总题数")
    private Integer totalAmount;

    /** 正确题数 */
    @Excel(name = "正确题数")
    private Integer rightAmount;

    /** 作答题数 */
    @Excel(name = "作答题数")
    private Integer answerAmount;

    /** 错误题数 */
    @Excel(name = "错误题数")
    private Integer errorAmount;

    /** 作答时长，单位秒 */
    @Excel(name = "作答时长，单位秒")
    private Integer durationSecond;

    /** 得分 */
    @Excel(name = "得分")
    private BigDecimal mark;

    /** 总分 */
    private BigDecimal totalMark;

    /** 状态 默认0-正常 1-暂存 */
    private Integer status;

    /** 题目列表 */
    @Excel(name = "题目列表")
    @JsonIgnore
    @TableField(typeHandler = TrainStudentAnswerListHandler.class)
    private List<TrainStudentAnswer> answerList;

    /** 开始答题时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "开始答题时间", width = 30, dateFormat = "yyyy-MM-dd")
    @TableField(updateStrategy = FieldStrategy.NEVER)
    private Date startTime;

    /** 提交时间 */
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Excel(name = "提交时间", width = 30, dateFormat = "yyyy-MM-dd")
    private Date submitTime;

    /** 做题类型 practice练习 test考试 */
    @Excel(name = "做题类型 practice练习 test考试")
    private String typeCode;

    /** 练习模式 answer：答题模式；recitation： */
    @Excel(name = "练习模式")
    private String practiceModeCode;

    /** 正确率 */
    @TableField(exist = false)
    private BigDecimal accuracy;

    /** 题目列表 */
    @TableField(exist = false)
    private List<QuestionRes> questionGroupList;

    /** 及格分数 */
    private BigDecimal passMark;

    /** 及格标签 1-是 0-否 */
    private Integer passedFlag;

    public BigDecimal getAccuracy()
    {
        if (this.rightAmount == null || this.totalAmount == null || this.totalAmount == 0
                || this.answerAmount == null || this.answerAmount == 0) {
            return BigDecimal.ZERO;
        }
        return TrainConst.Type.TEST.getCode().equals(this.getTypeCode()) ?
                new BigDecimal(this.rightAmount).divide(new BigDecimal(this.totalAmount), 2, RoundingMode.UP)
                : new BigDecimal(this.rightAmount).divide(new BigDecimal(this.answerAmount), 2, RoundingMode.UP);
    }

    public Train(Long studentId, Long courseId, String typeCode)
    {
        this.studentId = studentId;
        this.courseId = courseId;
        this.typeCode = typeCode;
    }

    public Train(String typeCode)
    {
        this.typeCode = typeCode;
    }

    public Train(String typeCode, String practiceModeCode)
    {
        this.typeCode = typeCode;
        this.practiceModeCode = practiceModeCode;
    }

    @Override
    @NotNull(message = "唯一主键不能为空", groups = {VEdit.class})
    public Long getId()
    {
        return super.getId();
    }

    public Integer getPassedFlag()
    {
        if (ObjectUtil.isNull(this.mark) || ObjectUtil.isNull(this.passMark)) {
            return SysConst.Flag.IS.getFlag();
        }
        return (this.mark.compareTo(this.passMark) < 0) ? SysConst.Flag.NO.getFlag() : SysConst.Flag.IS.getFlag();
    }
}
